package com.example.suze.libraryinformationmanagementsystem.Adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.suze.libraryinformationmanagementsystem.R;
import com.example.suze.libraryinformationmanagementsystem.net.entiry.BookDatum;
import com.person.entity.Book;

import java.util.List;

/**
 * Created by suze on 2017/6/12.
 */

public class BookListAdapter extends BaseAdapter {
    private Context context;

    private List<BookDatum> list=null;

    public BookListAdapter(Context context, List<BookDatum> list) {
        this.context = context;
        this.list = list;
    }

    /**
     * 传入新的数据 刷新UI的方法
     */
    public void updateListView(List<BookDatum> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        BookDatum mContent = list.get(position);
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.book_list, null);
            viewHolder.abstract1 = (TextView) convertView.findViewById(R.id.book_abstract);
            viewHolder.name = (TextView) convertView.findViewById(R.id.book_name);
            viewHolder.author= (TextView) convertView.findViewById(R.id.book_author);
            viewHolder.publishing= (TextView) convertView.findViewById(R.id.book_publishing);
            viewHolder.book = (ImageView) convertView.findViewById(R.id.book_img);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.abstract1.setText(mContent.getabstract1());
        viewHolder.name.setText(mContent.getbookname());
        viewHolder.author.setText(mContent.getauthor());
        viewHolder.publishing.setText(mContent.getpublishing());
        String img = mContent.getimg();
        Bitmap book = base64ToBitmap(img);
        viewHolder.book.setImageBitmap(book);
        return convertView;
    }

    class ViewHolder {
        ImageView book;
        TextView name;
        TextView author;
        TextView abstract1;
        TextView publishing;
    }
    /**
     * base64转bitmap
     *
     * @param base64String
     * @return
     */
    public static Bitmap base64ToBitmap(String base64String) {
        byte[] bytes = Base64.decode(base64String, Base64.DEFAULT);
        Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
        return bitmap;
    }
}
